package yyr.cn.com.observerpattern.publish;

import java.util.ArrayList;

import yyr.cn.com.observerpattern.suberscribe.Observer;

/**
 * Created by Administrator on 2018/2/12 0012.
 */

public class ZhangBoss  implements  Subject {
    public ArrayList<Observer>   subjects;

    @Override
    public void attach(Observer observer) {
        subjects = getInstance();
        subjects.add(observer);

    }

    @Override
    public void detach(Observer observer) {
        subjects.remove(observer);
    }

    @Override
    public void notification(String message) {
        if(subjects==null||subjects.size()==0){
            return;
        }
        int  countNumber=subjects.size();
        for (int i=0;i<countNumber;i++){
            Observer  observer= subjects.get(i);
            observer.update(message);
        }


    }

    public void  sendCommand(String   command){
        notification(command);
    }
    private ArrayList<Observer>  getInstance(){
        if(subjects==null){
            subjects=new  ArrayList<Observer>();
        }
        return subjects;
    }
}
